#include <iostream>
#include <vector>
#include <any>
#include <cstring>
#include "AnsysEDCOM.h"

int main() {
    AnsysEDCOM* ansysED = new AnsysEDCOM(L"Ansoft.ElectronicsDesktop");
    IDispatch* appDesktop = ansysED->getAppDesktop();
    IDispatch* oDesktop = ansysED->InvokeMethod(appDesktop, L"GetAppDesktop", {});

    ansysED->InvokeMethod(oDesktop, L"RestoreWindow", {});
    IDispatch* oProject = ansysED->InvokeMethod(oDesktop, L"NewProject", {});
    ansysED->InvokeMethod(oProject, L"SaveAs",
        {
            L"G:\\temp\\Prj_tmp\\maxwell_secondary_development\\tmp.aedt",
            true
        }
    );
    ansysED->InvokeMethod(oProject, L"InsertDesign",
        {
            L"Maxwell 2D", L"Maxwell2DDesign1", L"Electrostatic", L""
        }
    );
    IDispatch* oDesign = ansysED->InvokeMethod(oProject, L"SetActiveDesign",
        {
            L"Maxwell2DDesign1"
        }
    );
    IDispatch* oEditor = ansysED->InvokeMethod(oDesign, L"SetActiveEditor",
        {
            L"3D Modeler"
        }
    );
    ansysED->InvokeMethod(oEditor, L"CreateCircle",
        {
            Array{
                L"NAME:CircleParameters",
                L"IsCovered:="		, true,
                L"XCenter:="		, L"0mm",
                L"YCenter:="		, L"0mm",
                L"ZCenter:="		, L"0mm",
                L"Radius:="		, L"5mm",
                L"WhichAxis:="		, L"Z",
                L"NumSegments:="		, L"0"
            },
            Array{
                L"NAME:Attributes",
                L"Name:="		, L"Circle1",
                L"Flags:="		, L"",
                L"Color:="		, L"(143 175 143)",
                L"Transparency:="	, 0,
                L"PartCoordinateSystem:=", L"Global",
                L"UDMId:="		, L"",
                L"MaterialValue:="	, L"\"vacuum\"",
                L"SurfaceMaterialValue:=", L"\"\"",
                L"SolveInside:="		, true,
                L"ShellElement:="	, false,
                L"ShellElementThickness:=", L"0mm",
                L"IsMaterialEditable:="	, true,
                L"UseMaterialAppearance:=", false,
                L"IsLightweight:="	, false
            }
        }
    );
    ansysED->InvokeMethod(oEditor, L"CreateCircle",
        {
            Array{
                L"NAME:CircleParameters",
                L"IsCovered:="		, true,
                L"XCenter:="		, L"0mm",
                L"YCenter:="		, L"0mm",
                L"ZCenter:="		, L"0mm",
                L"Radius:="		, L"3mm",
                L"WhichAxis:="		, L"Z",
                L"NumSegments:="		, L"0"
            },
            Array{
                L"NAME:Attributes",
                L"Name:="		, L"Circle2",
                L"Flags:="		, L"",
                L"Color:="		, L"(143 175 143)",
                L"Transparency:="	, 0,
                L"PartCoordinateSystem:=", L"Global",
                L"UDMId:="		, L"",
                L"MaterialValue:="	, L"\"vacuum\"",
                L"SurfaceMaterialValue:=", L"\"\"",
                L"SolveInside:="		, true,
                L"ShellElement:="	, false,
                L"ShellElementThickness:=", L"0mm",
                L"IsMaterialEditable:="	, true,
                L"UseMaterialAppearance:=", false,
                L"IsLightweight:="	, false
            }
        }
    );
    ansysED->InvokeMethod(oEditor, L"Subtract",
        {
            Array{
                L"NAME:Selections",
                L"Blank Parts:=", L"Circle1",
                L"Tool Parts:=", L"Circle2"
            },
            Array{
                L"NAME:SubtractParameters",
                L"KeepOriginals:=", true
            }
        }
    );
    IDispatch* oModule = ansysED->InvokeMethod(oDesign, L"GetModule",
        {
            L"BoundarySetup"
        }
    );
    ansysED->InvokeMethod(oModule, L"AssignVoltage",
        {
            "",
            Array{
                L"NAME:Voltage1",
                L"Edges:=", Array{ 13 },
                L"Value:=", L"1V",
                L"CoordinateSystem:=", L""
            }
        }
    );
    ansysED->InvokeMethod(oModule, L"AssignVoltage",
        {
            "",
            Array{
                L"NAME:Voltage2",
                L"Edges:=", Array{ 7 },
                L"Value:=", L"0V",
                L"CoordinateSystem:=", L""
            }
        }
    );
    oModule = ansysED->InvokeMethod(oDesign, L"GetModule", { L"AnalysisSetup" });
    ansysED->InvokeMethod(oModule, L"InsertSetup",
        {
            L"Electrostatic",
            Array{
                L"NAME:Setup1",
                L"Enabled:=", true,
                Array{
                    L"NAME:MeshLink",
                    L"ImportMesh:=", false
                },
                L"MaximumPasses:=", 10,
                L"MinimumPasses:=", 2,
                L"MinimumConvergedPasses:=", 1,
                L"PercentRefinement:=", 30,
                L"SolveFieldOnly:=", false,
                L"PercentError:=", 1,
                L"SolveMatrixAtLast:=", true,
                L"UseNonLinearIterNum:=", false,
                L"NonLinearResidual:=", 0.001
            }
        }
    );
    ansysED->InvokeMethod(oDesign, L"AnalyzeAll", {});
    oModule = ansysED->InvokeMethod(oDesign, L"GetModule", {L"FieldsReporter"});
    ansysED->InvokeMethod(oModule, L"CreateFieldPlot", 
        {
            Array{
                L"NAME:Voltage1",
                L"SolutionName:=", L"Setup1 : LastAdaptive",
                L"UserSpecifyName:=", 0,
                L"UserSpecifyFolder:=", 0,
                L"QuantityName:=", L"Voltage",
                L"PlotFolder:=", L"Voltage",
                L"StreamlinePlot:=", false,
                L"AdjacentSidePlot:=", false,
                L"FullModelPlot:=", false,
                L"IntrinsicVar:=", L"",
                L"PlotGeomInfo:=", Array{ 1,L"Surface",L"FacesList",2,L"6",L"12" },
                L"FilterBoxes:=", Array{ 0 },
                Array{
                    L"NAME:PlotOnSurfaceSettings",
                    L"Filled:=", false,
                    L"IsoValType:=", L"Tone",
                    L"AddGrid:=", false,
                    L"MapTransparency:=", true,
                    L"Refinement:=", 0,
                    L"Transparency:=", 0,
                    L"SmoothingLevel:=", 0,
                    L"ShadingType:=", 0,
                    Array{
                        L"NAME:Arrow3DSpacingSettings",
                        L"ArrowUniform:=", true,
                        L"ArrowSpacing:=", 0,
                        L"MinArrowSpacing:=", 0,
                        L"MaxArrowSpacing:=", 0
                    },
                    L"GridColor:=", Array{ 255,255,255 }
                },
                L"EnableGaussianSmoothing:=", false,
                L"SurfaceOnly:=", false
            },
            L"Field"
        });
    ansysED->InvokeMethod(oProject, L"Save", {});

    delete ansysED;
    return 0;
}


